package com.sspku.weinotes.controller;

import com.sspku.weinotes.entity.NoteBook;
import com.sspku.weinotes.exception.IncorrectParameterException;
import com.sspku.weinotes.exception.NoSuchIdException;
import com.sspku.weinotes.response.ServerResponse;
import com.sspku.weinotes.security.SecurityUtils;
import com.sspku.weinotes.service.NoteBookService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
public class NoteBookController {
    private static final Logger logger = LoggerFactory.getLogger(NoteBookController.class);
    @Autowired
    private NoteBookService noteBookService;


    /**
     * 获取所有笔记本信息集合
     *
     * @return 笔记本集合
     */
    @GetMapping("/note_books")
    public ServerResponse getAllNoteBooks() {
        Integer uid= SecurityUtils.getUser().getUid();
        try{
            List<NoteBook> list=noteBookService.getAllNoteBook(uid);
            return new ServerResponse(list);
        }catch (NoSuchIdException e)
        {
            logger.info("get all note books exception " + e);
            return new ServerResponse(400,"对应用户不存在");
        }
    }

    /**
     * 新建笔记本
     *
     * @param name     笔记本名
     * @param response {@link HttpServletResponse}
     */
    @PostMapping("/note_book")
    public void newNoteBook(String name, HttpServletResponse response) {
        try {
            Integer uid= SecurityUtils.getUser().getUid();
            noteBookService.addNoteBook(uid, name);
            response.setStatus(201);
        } catch (IncorrectParameterException e) {
            logger.info("new note book exception " + e);
            response.setStatus(400);
        }
    }

    /**
     * 修改笔记本
     *
     * @param id       笔记本ID
     * @param name     新笔记本名
     * @param response {@link HttpServletResponse}
     */
    @PatchMapping("/note_book/{id}/{name}")
    public void upNoteBook(@PathVariable("id") int id, @PathVariable("name") String name, HttpServletResponse response) {
        try {
            noteBookService.modifyNoteBookName(id, name);
            response.setStatus(200);
        } catch (NoSuchIdException e) {
            logger.info("modify note book exception " + e);
            response.setStatus(404);
        } catch (IncorrectParameterException e) {
            logger.info("modify note book exception " + e);
            response.setStatus(400);
        }
    }

    /**
     * 删除笔记本
     *
     * @param id       笔记本ID
     * @param response {@link HttpServletResponse}
     */
    @DeleteMapping("/note_book/{id}")
    public void delNoteBook(@PathVariable("id") int id, HttpServletResponse response) {
        try {
            noteBookService.deleteNoteBookById(id);
            response.setStatus(200);
        } catch (NoSuchIdException e) {
            logger.info("delete note book exception " + e);
            response.setStatus(400);
        }
    }

}
